package gui.panel_new.evaluation.student;

import enums.TextBundleEnum;
import enums.URLEnum;
import gui.panel_new.evaluation.exeptions.ExerciseNotBuiltException;
import gui.panel_new.evaluation.exeptions.PunctuationZeroException;
import misc.BundlesProvider;
import model.eval.TestExcercise;
import model.mchoice.MCItem;
import model.mchoice.MCItemSet;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ResourceBundle;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * @author Jose Martin Rozanec;
 */
public class MChoicePanel extends ExercisePanel {
    private JLabel questionLabel;
    private JTextField questionTextField;
    private JLabel answersLabel;
    private JLabel firstAnswerLabel;
    private JLabel secondAnswerLabel;
    private JLabel thirdAnswerLabel;
    private JLabel fourthAnswerLabel;
    private JLabel fifthAnswerLabel;
    private JLabel sixthAnswerLabel;
    private JTextField firstAnswerTextField;
    private JTextField secondAnswerTextField;
    private JTextField thirdAnswerTextField;
    private JTextField fourthAnswerTextField;
    private JTextField fifthAnswerTextField;
    private JTextField sixthAnswerTextField;
    private JLabel correctLabel;
    private JCheckBox correct1;
    private JCheckBox correct2;
    private JCheckBox correct3;
    private JCheckBox correct4;
    private JCheckBox correct5;
    private JCheckBox correct6;
    private JLabel addsPoints;
    private JLabel substractPoints;
    private JTextField addsPointsField;
    private JTextField substractPointsField;
    private JButton nextQuestionButton;
    private JButton previousQuestionButton;
    private JTextArea fullQuestionaireTextArea;
    private JTextField questionsQuantityTextField;
    private JCheckBox proportionalPunctuationCheckBox;
    private JCheckBox oneAnswerPerQuestionCheckBox;
    private JTextField substractProportionalityTextField;
    private ButtonGroup buttonGroup;
    private JPanel questionsPanel;
    private ResourceBundle textBundle;

    private MCItemSet mcItemSet;
    private MCItemSet studentSolutionMCSet;
    private MCItem currentDataItem;

    public MChoicePanel() {
        initComponents();
        initGUI();
    }

    private void initComponents() {
        textBundle = BundlesProvider.getBundle(URLEnum.TEXT_BUNDLE);
        questionLabel = new JLabel(textBundle.getString(TextBundleEnum.PREGUNTA.toString()));
        questionTextField = new JTextField(44);
        answersLabel = new JLabel(textBundle.getString(TextBundleEnum.ANSWERS.toString()));
        firstAnswerLabel = new JLabel("A- ");
        secondAnswerLabel = new JLabel("B- ");
        thirdAnswerLabel = new JLabel("C- ");
        fourthAnswerLabel = new JLabel("D- ");
        fifthAnswerLabel = new JLabel("E- ");
        sixthAnswerLabel = new JLabel("F- ");
        firstAnswerTextField = new JTextField(25);
        secondAnswerTextField = new JTextField(25);
        thirdAnswerTextField = new JTextField(25);
        fourthAnswerTextField = new JTextField(25);
        fifthAnswerTextField = new JTextField(25);
        sixthAnswerTextField = new JTextField(25);
        correctLabel = new JLabel(textBundle.getString(TextBundleEnum.CORRECTA.toString()));
        correct1 = new JCheckBox();
        correct2 = new JCheckBox();
        correct3 = new JCheckBox();
        correct4 = new JCheckBox();
        correct5 = new JCheckBox();
        correct6 = new JCheckBox();
        addCheckBoxActionListeners();
        addsPoints = new JLabel(textBundle.getString(TextBundleEnum.SUMA.toString()));
        substractPoints = new JLabel(textBundle.getString(TextBundleEnum.RESTA.toString()));
        addsPointsField = new JTextField(7);
        substractPointsField = new JTextField(7);
        nextQuestionButton = new JButton(textBundle.getString(TextBundleEnum.NEXT.toString()));
        previousQuestionButton = new JButton(textBundle.getString(TextBundleEnum.PREVIOUS.toString()));
        fullQuestionaireTextArea = new JTextArea(16, 48);// rows;columns//8
        questionsQuantityTextField = new JTextField(32);
        questionsQuantityTextField.setText("10");
        proportionalPunctuationCheckBox = new JCheckBox(
                textBundle.getString(TextBundleEnum.FIXED_PUNCTUATION.toString()));
        oneAnswerPerQuestionCheckBox = new JCheckBox(textBundle.getString(
                TextBundleEnum.ONE_ANSWER_PER_QUESTION.toString()));
        substractProportionalityTextField = new JTextField(22);
        substractProportionalityTextField.setText("0.5");
        substractProportionalityTextField.setToolTipText(
                textBundle.getString(
                        TextBundleEnum.SUBSTRACT_PER_QUESTION_PROPORTIONALITY_TOOLTIP.toString()));
        buttonGroup = new ButtonGroup();
        questionsPanel = new JPanel();
        addButtonsActionListeners();
        setButtonsSize(nextQuestionButton);
        setButtonsSize(previousQuestionButton);
    }

    private void initGUI() {
        setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        add(createQuestionCreationPanel(), gridBagConstraints);
        enableQuestionsPanel(true);
    }

    private JPanel createQuestionCreationPanel() {
        int x = 0;
        int y = 0;
        questionsPanel.setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        fullQuestionaireTextArea.setEditable(false);
        questionsPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.BLACK),
                textBundle.getString(TextBundleEnum.PREGUNTA.toString()) + " 1 " +
                        textBundle.getString(TextBundleEnum.DE.toString()) + " " + questionsQuantityTextField.getText()));
        gridBagConstraints.insets = new Insets(5, 5, 5, 5);
        questionsPanel.add(questionLabel, gridBagConstraints);
        gridBagConstraints.gridx = ++x;
        gridBagConstraints.gridwidth = 3;
        questionsPanel.add(questionTextField, gridBagConstraints);
        gridBagConstraints.gridy = ++y;
        gridBagConstraints.gridx = --x;
        gridBagConstraints.gridwidth = 2;
        questionsPanel.add(answersLabel, gridBagConstraints);
        x = 2;
        gridBagConstraints.gridx = x;
        gridBagConstraints.gridwidth = 1;
        questionsPanel.add(correctLabel, gridBagConstraints);
        gridBagConstraints.gridheight = 5;//3
        gridBagConstraints.gridx = ++x;
        questionsPanel.add(createScoringPanel(), gridBagConstraints);
        x = 0;
        gridBagConstraints.gridx = x;
        gridBagConstraints.gridy = ++y;
        gridBagConstraints.gridheight = 1;
        questionsPanel.add(firstAnswerLabel, gridBagConstraints);
        gridBagConstraints.gridx = ++x;
        questionsPanel.add(firstAnswerTextField, gridBagConstraints);
        gridBagConstraints.gridx = ++x;
        questionsPanel.add(correct1, gridBagConstraints);
        x = 0;
        gridBagConstraints.gridx = x;
        gridBagConstraints.gridy = ++y;
        questionsPanel.add(secondAnswerLabel, gridBagConstraints);
        gridBagConstraints.gridx = ++x;
        questionsPanel.add(secondAnswerTextField, gridBagConstraints);
        gridBagConstraints.gridx = ++x;
        questionsPanel.add(correct2, gridBagConstraints);
        x = 0;
        gridBagConstraints.gridx = x;
        gridBagConstraints.gridy = ++y;
        questionsPanel.add(thirdAnswerLabel, gridBagConstraints);
        gridBagConstraints.gridx = ++x;
        questionsPanel.add(thirdAnswerTextField, gridBagConstraints);
        gridBagConstraints.gridx = ++x;
        questionsPanel.add(correct3, gridBagConstraints);
        x = 0;
        gridBagConstraints.gridx = x;
        gridBagConstraints.gridy = ++y;
        questionsPanel.add(fourthAnswerLabel, gridBagConstraints);
        gridBagConstraints.gridx = ++x;
        questionsPanel.add(fourthAnswerTextField, gridBagConstraints);
        gridBagConstraints.gridx = ++x;
        questionsPanel.add(correct4, gridBagConstraints);
        x = 0;
        gridBagConstraints.gridx = x;
        gridBagConstraints.gridy = ++y;
        questionsPanel.add(fifthAnswerLabel, gridBagConstraints);
        gridBagConstraints.gridx = ++x;
        questionsPanel.add(fifthAnswerTextField, gridBagConstraints);
        gridBagConstraints.gridx = ++x;
        questionsPanel.add(correct5, gridBagConstraints);
        x = 0;
        gridBagConstraints.gridx = x;
        gridBagConstraints.gridy = ++y;
        questionsPanel.add(sixthAnswerLabel, gridBagConstraints);
        gridBagConstraints.gridx = ++x;
        questionsPanel.add(sixthAnswerTextField, gridBagConstraints);
        gridBagConstraints.gridx = ++x;
        questionsPanel.add(correct6, gridBagConstraints);
        x = 0;
        gridBagConstraints.gridx = x;
        gridBagConstraints.gridy = ++y;
        gridBagConstraints.gridwidth = 4;
        questionsPanel.add(createButtonsPanel(), gridBagConstraints);
        gridBagConstraints.gridy = ++y;
        gridBagConstraints.gridheight = 9;//6
        questionsPanel.add(createFullQuestionairePanel(), gridBagConstraints);
        return questionsPanel;
    }

    private JPanel createScoringPanel() {
        JPanel scoringPanel = new JPanel();
        scoringPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.BLACK),
                textBundle.getString(TextBundleEnum.PUNTAJE.toString())));
        scoringPanel.setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(5, 5, 5, 5);
        scoringPanel.add(addsPoints, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        scoringPanel.add(addsPointsField, gridBagConstraints);
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridx = 0;
        scoringPanel.add(substractPoints, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        scoringPanel.add(substractPointsField, gridBagConstraints);
        return scoringPanel;
    }

    private JPanel createButtonsPanel() {
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setLayout(new GridBagLayout());
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.insets = new Insets(0, 25, 0, 25);
        gridBagConstraints.gridx = 0;
        buttonsPanel.add(previousQuestionButton, gridBagConstraints);
        gridBagConstraints.gridx = 1;
        buttonsPanel.add(nextQuestionButton, gridBagConstraints);
        return buttonsPanel;
    }

    private JScrollPane createFullQuestionairePanel() {    //todo
        JScrollPane scrollPane = new JScrollPane(fullQuestionaireTextArea);
        scrollPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK),
                textBundle.getString(TextBundleEnum.CUESTIONARIO.toString())));
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        return scrollPane;
    }

    private void addButtonsActionListeners() {
        nextQuestionButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setCurrentSolution();
                studentSolutionMCSet.next();
                currentDataItem = mcItemSet.next();
                setQuestionData();
            }
        });

        previousQuestionButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setCurrentSolution();
                studentSolutionMCSet.previous();
                currentDataItem = mcItemSet.previous();
                setQuestionData();
            }
        });
    }

    private void enableAnswerTextFields(boolean first, boolean second, boolean third, boolean fourth,
                                        boolean fifth, boolean sixth) {
        firstAnswerTextField.setEnabled(first);
        correct1.setEnabled(first);
        secondAnswerTextField.setEnabled(second);
        correct2.setEnabled(second);
        thirdAnswerTextField.setEnabled(third);
        correct3.setEnabled(third);
        fourthAnswerTextField.setEnabled(fourth);
        correct4.setEnabled(fourth);
        fifthAnswerTextField.setEnabled(fifth);
        correct5.setEnabled(fifth);
        sixthAnswerTextField.setEnabled(sixth);
        correct6.setEnabled(sixth);
    }

    private void manageButtonGroup() {
        if (mcItemSet.isOneCorrectAnswerOnly()) {
            buttonGroup.add(correct1);
            buttonGroup.add(correct2);
            buttonGroup.add(correct3);
            buttonGroup.add(correct4);
            buttonGroup.add(correct5);
            buttonGroup.add(correct6);
        } else {
            buttonGroup.remove(correct1);
            buttonGroup.remove(correct2);
            buttonGroup.remove(correct3);
            buttonGroup.remove(correct4);
            buttonGroup.remove(correct5);
            buttonGroup.remove(correct6);
        }
    }

    private void enableQuestionsPanel(boolean enable) {
//        fullQuestionaireTextArea.setEnabled(enable);
        correct1.setEnabled(enable);
        correct2.setEnabled(enable);
        correct3.setEnabled(enable);
        correct4.setEnabled(enable);
        correct5.setEnabled(enable);
        correct6.setEnabled(enable);
        firstAnswerTextField.setEditable(false);
        secondAnswerTextField.setEditable(false);
        thirdAnswerTextField.setEditable(false);
        fourthAnswerTextField.setEditable(false);
        fifthAnswerTextField.setEditable(false);
        sixthAnswerTextField.setEditable(false);
        questionTextField.setEditable(false);
        substractPointsField.setEditable(false);
        addsPointsField.setEditable(false);
    }

    private void setButtonsSize(JButton button) {
        button.setPreferredSize(new Dimension(100, 25));
    }

    public MCItemSet getMCItemSet() {
        return mcItemSet;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setContentPane(new MChoicePanel());
        frame.setSize(new Dimension(500, 500));
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public TestExcercise getExercise() throws ExerciseNotBuiltException, PunctuationZeroException {
        if (mcItemSet.getMCItems().size() < Integer.parseInt(questionsQuantityTextField.getText())
                || addsPointsField.getText().equals("") || substractPointsField.getText().equals("")) {
            throw new ExerciseNotBuiltException();
        }
        if (Integer.parseInt(addsPointsField.getText()) == 0) {
            throw new PunctuationZeroException();
        }
        return testExercise;
    }

    public void setExercise(TestExcercise testExerciseData) {
        enableQuestionsPanel(true);
        testExercise = testExerciseData;
        this.mcItemSet = (MCItemSet) testExercise.getExcerciseData();
        this.studentSolutionMCSet = (MCItemSet) testExercise.getStudentsSolution();
        fullQuestionaireTextArea.setText(studentSolutionMCSet.toString());
        fullQuestionaireTextArea.setCaretPosition(0);
        currentDataItem = mcItemSet.getMCItems().get(0);
        manageButtonGroup();
        addsPointsField.setText("" + currentDataItem.getCorrectPunctuation());
        substractPointsField.setText("" + currentDataItem.getIncorrectPunctuation());
        questionTextField.setText(currentDataItem.getQuestion());
        String[] solutions = currentDataItem.getSolutions();
        firstAnswerTextField.setText(solutions[0]);
        secondAnswerTextField.setText(solutions[1]);
        thirdAnswerTextField.setText(solutions[2]);
        enableAnswerTextFields(false, false, false, false, false, false);
        disableCheckBoxes();
        if (solutions.length > 3) {
            fourthAnswerTextField.setText(solutions[3]);
            enableCheckBoxes(correct4, correct5, correct6, 4);
        }
        if (solutions.length > 4) {
            fifthAnswerTextField.setText(solutions[4]);
            enableCheckBoxes(correct4, correct5, correct6, 5);
        }
        if (solutions.length > 5) {
            sixthAnswerTextField.setText(solutions[5]);
            enableCheckBoxes(correct4, correct5, correct6, 6);
        }
        markCheckBoxes(studentSolutionMCSet.current().getSolutionStatus());
        questionsPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.BLACK),
                textBundle.getString(TextBundleEnum.PREGUNTA.toString()) +
                        " " + (mcItemSet.getCurrentPosition() + 1) + " " +
                        textBundle.getString(TextBundleEnum.DE.toString()) + " " +
                        mcItemSet.getTotalItems()));
    }

    private static void enableCheckBoxes(JCheckBox box4, JCheckBox box5, JCheckBox box6, int questions) {
        box4.setEnabled(false);
        box5.setEnabled(false);
        box6.setEnabled(false);
        switch (questions) {
            case 6:
                box6.setEnabled(true);
            case 5:
                box5.setEnabled(true);
            case 4:
                box4.setEnabled(true);
        }
    }

    private void disableCheckBoxes() {
        correct1.setEnabled(true);
        correct2.setEnabled(true);
        correct3.setEnabled(true);

        correct4.setEnabled(false);
        correct5.setEnabled(false);
        correct6.setEnabled(false);
    }

    private static void enableTextFields(JTextField textField4, JTextField textField5,
                                         JTextField textField6, int questions) {//todo delete
        textField4.setEnabled(false);
        textField5.setEnabled(false);
        textField6.setEnabled(false);
        switch (questions) {
            case 6:
                textField6.setEnabled(true);
            case 5:
                textField5.setEnabled(true);
            case 4:
                textField4.setEnabled(true);
        }
    }

    private void setCurrentSolution() {
        currentDataItem = mcItemSet.current();
        studentSolutionMCSet.current().setSolutionStatus(correct1.isSelected(), 1);
        studentSolutionMCSet.current().setSolutionStatus(correct2.isSelected(), 2);
        studentSolutionMCSet.current().setSolutionStatus(correct3.isSelected(), 3);
        if (currentDataItem.getSolutionStatus().length > 3) {
            studentSolutionMCSet.current().setSolutionStatus(correct4.isSelected(), 4);
        }
        if (currentDataItem.getSolutionStatus().length > 4) {
            studentSolutionMCSet.current().setSolutionStatus(correct5.isSelected(), 5);
        }
        if (currentDataItem.getSolutionStatus().length > 5) {
            studentSolutionMCSet.current().setSolutionStatus(correct6.isSelected(), 6);
        }
    }

    private void setQuestionData() {
        int answers = currentDataItem.getSolutions().length;
        questionTextField.setText(currentDataItem.getQuestion());
        firstAnswerTextField.setText(currentDataItem.getSolutions()[0]);
        secondAnswerTextField.setText(currentDataItem.getSolutions()[1]);
        thirdAnswerTextField.setText(currentDataItem.getSolutions()[2]);

        boolean[] solutionStatus = studentSolutionMCSet.current().getSolutionStatus();
        correct1.setSelected(solutionStatus[0]);
        correct2.setSelected(solutionStatus[1]);
        correct3.setSelected(solutionStatus[2]);


        sixthAnswerLabel.setText("");
        fifthAnswerTextField.setText("");
        fourthAnswerTextField.setText("");
        enableCheckBoxes(correct4, correct5, correct6, answers);
        if (answers > 3) {
            switch (answers) {
                case 6:
                    sixthAnswerLabel.setText(currentDataItem.getSolutions()[5]);
                    correct6.setSelected(solutionStatus[5]);
                case 5:
                    fifthAnswerTextField.setText(currentDataItem.getSolutions()[4]);
                    correct5.setSelected(solutionStatus[4]);
                case 4:
                    fourthAnswerTextField.setText(currentDataItem.getSolutions()[3]);
                    correct4.setSelected(solutionStatus[3]);
            }
        }


        addsPointsField.setText("" + currentDataItem.getCorrectPunctuation());
        substractPointsField.setText("" + currentDataItem.getIncorrectPunctuation());

        questionsPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.BLACK),
                textBundle.getString(TextBundleEnum.PREGUNTA.toString()) +
                        " " + (mcItemSet.getCurrentPosition() + 1) + " " +
                        textBundle.getString(TextBundleEnum.DE.toString()) + " " +
                        mcItemSet.getTotalItems()));
        fullQuestionaireTextArea.setText(studentSolutionMCSet.toString());
        fullQuestionaireTextArea.setCaretPosition(0);
        questionTextField.requestFocus();
    }

    private void addCheckBoxActionListeners() {
        addCheckBoxActionListener(correct1, 1);
        addCheckBoxActionListener(correct2, 2);
        addCheckBoxActionListener(correct3, 3);
        addCheckBoxActionListener(correct4, 4);
        addCheckBoxActionListener(correct5, 5);
        addCheckBoxActionListener(correct6, 6);
    }

    private void addCheckBoxActionListener(final JCheckBox checkBox, final int answer) {
        checkBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                studentSolutionMCSet.current().setSolutionStatus(checkBox.isSelected(), answer);
            }
        });
    }

    private void markCheckBoxes(boolean[] array) {
        correct1.setSelected(array[0]);
        correct2.setSelected(array[1]);
        correct3.setSelected(array[2]);
        if (array.length > 3) {
            correct4.setSelected(array[3]);
        }
        if (array.length > 4) {
            correct5.setSelected(array[4]);
        }
        if (array.length > 5) {
            correct6.setSelected(array[5]);
        }
    }
}
